package com.symaster.common.util;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author yinmiao
 * @date 2021-04-13 20:58
 */
public class ArrayUtil {

    private static Random random;

    /**
     * 移除指定元素
     *
     * @param src 原数组
     * @param des 需要移除的内容
     */
    public static String[] removeAll(String[] src, String des) {
        for (int i = 0; i < src.length; i++) {
            if (StringUtils.isEmpty(src[i])) {
                if (i + 1 >= src.length) {
                    src[i] = null;
                    continue;
                }
                src[i] = src[i + 1];
                src[i + 1] = null;
                continue;
            }

            if (src[i].split(":")[0].equals(des)) {
                if (i + 1 >= src.length) {
                    src[i] = null;
                    continue;
                }
                src[i] = src[i + 1];
                src[i + 1] = null;
            }
        }
        return src;
    }

    /**
     * 返回随机且不重复数组
     */
    public static int[] getRandomArrays(int length) {
        int[] a1 = new int[length];

        for (int i = 0; i < a1.length; i++) {
            a1[i] = i;
        }

        return getRandomArrays(a1);
    }

    /**
     * 打乱数组元素存储顺序
     */
    public static int[] getRandomArrays(int[] a1) {
        if (a1.length <= 1) {
            return a1;
        }

        int t;
        for (int i = 0; i < a1.length - 1; i++) {
            int a = 0;
            int b = 0;
            while (a == b) {
                if (random == null) {
                    random = new Random();
                }
                a = random.nextInt(a1.length);
                b = random.nextInt(a1.length);
            }
            t = a1[a];
            a1[a] = a1[b];
            a1[b] = t;
        }
        return a1;
    }

    /**
     * 打乱数组元素存储顺序
     */
    public static Integer[] getRandomArrays(Integer[] a1) {

        if (a1.length == 1) {
            return a1;
        }

        Integer t;
        for (int i = 0; i < a1.length; i++) {
            int a = 0;
            int b = 0;
            while (a == b) {
                a = new Random().nextInt(a1.length);
                b = new Random().nextInt(a1.length);
            }
            t = a1[a];
            a1[a] = a1[b];
            a1[b] = t;
        }
        return a1;
    }

    /**
     * 打乱数组元素存储顺序
     */
    public static List<Integer> getRandomArrays(List<Integer> a1) {

        if (a1.size() <= 1) {
            return a1;
        }

        Integer t;
        for (int i = 0; i < a1.size(); i++) {
            int a = 0;
            int b = 0;
            while (a == b) {
                a = new Random().nextInt(a1.size());
                b = new Random().nextInt(a1.size());
            }
            t = a1.get(a);
            a1.set(a, a1.get(b));
            a1.set(b, t);
        }
        return a1;
    }

    /**
     * 返回剩余数字
     */
    public static int[] surplus(int[] src, int len) {
        List<Integer> collect = Arrays.stream(src).boxed().collect(Collectors.toList());
        ArrayList<Integer> integers = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            if (collect.contains(i)) {
                continue;
            }
            integers.add(i);
        }

        return integers.stream().mapToInt(Integer::valueOf).toArray();
    }

    /**
     * 返回剩余数字
     */
    public static Integer[] surplus(Integer[] src, int len) {
        List<Integer> integers1 = Arrays.asList(src);
        if (src.length == len) {
            for (int i2 = 0; i2 < src.length; i2++) {
                if (src[i2] != null) {
                    continue;
                }
                int n = 0;
                while (true) {
                    int i = new Random().nextInt(len);
                    if (integers1.contains(i)) {
                        continue;
                    }
                    n = i;
                    break;
                }
                integers1.set(i2, n);
                src[i2] = n;
            }
            return src;
        }
        ArrayList<Integer> integers = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            if (integers1.contains(i)) {
                continue;
            }
            integers.add(i);
        }

        return integers.toArray(new Integer[0]);
    }

    /**
     * 返回剩余数字
     */
    public static List<Integer> surplus(List<Integer> src, int len) {
        List<Integer> integers = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            if (src.contains(i)) {
                continue;
            }
            integers.add(i);
        }

        return integers;
    }

    /**
     * 补充完整数组
     */
    public static int[] replenish(int[] src, int len) {
        int[] result = new int[len];
        System.arraycopy(src, 0, result, 0, src.length);
        for (int i = 0; i < result.length; i++) {
            arrayAdd(result, (src.length + i), i, (true));
        }
        return result;
    }

    public static double[] arrayAppend(double[] d1, double[] d2) {
        if (d1 == null) {
            return d2;
        }
        double[] doubles = new double[d1.length + d2.length];
        System.arraycopy(d1, 0, doubles, 0, d1.length);
        System.arraycopy(d2, 0, doubles, d1.length, d2.length);
        return doubles;
    }

    public static void arrayAdd(int[] src, int index, int n, boolean repetition) {
        if (!repetition) {
            src[index] = n;
        }
        for (int i = 0; i < index; i++) {
            if (src[i] == n) {
                return;
            }
        }
        src[index] = n;
    }
}
